package com.example.demovalidator.Vo;

import com.example.demovalidator.enums.ErrorCode;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * @author lxw
 * @date 2021/8/9
 * @apiNote
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultVo {
    /**
     * 后端是否处理成功
     */
    private boolean success;
    /**
     * 错误码
     */
    private String code;
    /**
     * 错误消息
     */
    private String msg;
    /**
     * 给前端返回的值
     */
    private Object data;

    public static ResultVo success(){
        ResultVo resultVo = new ResultVo();
        resultVo.setSuccess(true);
        return resultVo;
    }

    public static ResultVo success(Object data){
        ResultVo resultVo = new ResultVo();
        resultVo.setSuccess(true);
        resultVo.setData(data);
        return resultVo;
    }

    public static ResultVo fail(ErrorCode errorCode,Object data){
        ResultVo resultVo = new ResultVo();
        resultVo.setSuccess(false);
        resultVo.setCode(errorCode.getCode());
        resultVo.setMsg(errorCode.getMsg());
        resultVo.setData(data);
        return resultVo;
    }

    public static ResultVo fail(ErrorCode errorCode){
        ResultVo resultVo = new ResultVo();
        resultVo.setSuccess(false);
        resultVo.setCode(errorCode.getCode());
        resultVo.setMsg(errorCode.getMsg());
        return resultVo;
    }
}
